package main

import (
	"hero-league-lol-match-data/models"
	"hero-league-lol-match-data/pages"
	"hero-league-lol-match-data/tables"
	"log"
	"os"
	"os/signal"

	_ "github.com/GoAdminGroup/go-admin/adapter/gf"               // web framework adapter
	_ "github.com/GoAdminGroup/go-admin/modules/db/drivers/mysql" // sql driver
	"github.com/GoAdminGroup/go-admin/template"
	"github.com/GoAdminGroup/go-admin/template/chartjs"
	_ "github.com/GoAdminGroup/themes/adminlte" // ui theme

	"github.com/GoAdminGroup/go-admin/engine"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"

	_ "github.com/GoAdminGroup/themes/adminlte/separation"
)

func main() {
	startServer()
}

type Selectisit struct {
	Text  interface{} `json:"Text"`
	Value interface{} `json:"Value"`
}
type FieldOption struct {
	Text  string `json:"text"`
	Value string `json:"value"`
	// TextHTML      template.HTML     `json:"-"`
	// Selected      bool              `json:"-"`
	// SelectedLabel template.HTML     `json:"-"`
	// Extra         map[string]string `json:"-"`
}
type Selectisits []FieldOption

func (fo Selectisits) Selectisitfz() Selectisits {
	fo[0].Text = "测试"
	fo[0].Value = "1"
	return fo
}
func startServer() {
	s := g.Server()

	template.AddComp(chartjs.NewChart())

	eng := engine.Default()

	if err := eng.AddConfigFromYAML("./config.yml").
		AddGenerators(tables.Generators).
		Use(s); err != nil {
		panic(err)
	}

	eng.HTML("GET", "/admin", pages.GetDashBoard)
	eng.HTMLFile("GET", "/admin/hello", "./html/hello.tmpl", map[string]interface{}{
		"msg": "Hello world, do you",
	})

	lol_hero_brand, _ := g.DB().Model("lol_hero_brand").All()
	t := new(Selectisit)
	st := make(map[int]Selectisit)
	// var st Selectisits
	// st := make(map[int]Selectisits)
	for k, v := range lol_hero_brand {
		t.Text = v["title"]
		t.Value = v["id"]
		tt := *t
		// var tt Selectisit
		// tt = *t
		st[k] = tt
	}
	s.BindHandler("/lol_hero_brand", func(r *ghttp.Request) {
		r.Response.Write(st)
	})
	// tr := new(Selectisit)
	// var sr Selectisits
	// for k, v := range lol_hero_brand {
	// 	tr.Text = v["title"]
	// 	tr.Value = v["id"]
	// 	sr[k] = *tr
	// }
	sr := Selectisits.Selectisitfz
	srr := sr
	// sr.Value = "1"
	s.BindHandler("/lol_hero_brand_sr", func(r *ghttp.Request) {
		r.Response.Write(srr)
	})
	// fmt.Println("sr", sr)
	s.BindHandler("/", func(r *ghttp.Request) {
		r.Response.WriteTpl("./html/index.tmpl", g.Map{
			"msg":            "欢迎来到英雄联盟",
			"lol_hero_brand": lol_hero_brand,
		})
	})

	models.Init(eng.MysqlConnection())

	s.AddStaticPath("/uploads", "./uploads")

	s.SetPort(8098)
	go s.Run()

	quit := make(chan os.Signal, 1)
	signal.Notify(quit, os.Interrupt)
	<-quit
	log.Print("closing database connection")
	eng.MysqlConnection().Close()
}
